/*
 *  Copyright 2008-2009 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file jacobi.h
 *  \brief Jacobi relaxation.
 */

#pragma once

#include <cusp/detail/config.h>

#include <cusp/linear_operator.h>
#include <cusp/multi_array1d.h>

namespace cusp
{
namespace relaxation
{

template <typename ValueType, typename MemorySpace>
class multi_jacobi
{
public:
    ValueType default_omega;
    cusp::multi_array1d<ValueType,MemorySpace> diagonal;
    cusp::multi_array1d<ValueType,MemorySpace> temp;
    cusp::multi_array1d<ValueType,MemorySpace> x2;

    multi_jacobi();

    multi_jacobi(const multi_jacobi& other_jacobi) 
	: temp(other_jacobi.temp), diagonal(other_jacobi.diagonal), x2(other_jacobi.x2),
	  default_omega(other_jacobi.default_omega){}

    template <typename MatrixType>
    multi_jacobi(const MatrixType& A, ValueType omega=1.0);

    template<typename MatrixType, typename PartitionType>
    multi_jacobi(const MatrixType& A, ValueType omega, PartitionType &partition);

    // ignores initial x
    template<typename MatrixType, typename VectorType1, typename VectorType2>
    void presmooth(const MatrixType& A, const VectorType1& b, VectorType2& x);

    // smooths initial x
    template<typename MatrixType, typename VectorType1, typename VectorType2>
    void postsmooth(const MatrixType& A, const VectorType1& b, VectorType2& x);

    template <typename MatrixType, typename VectorType1, typename VectorType2>
    void operator()(const MatrixType& A, const VectorType1& b, VectorType2& x);

    template <typename MatrixType, typename VectorType1, typename VectorType2>
    void operator()(const MatrixType& A, const VectorType1& b, VectorType2& x, ValueType omega);

    template< typename Smoother >
    multi_jacobi &operator=(const Smoother& smoother)
    {
	x2 = smoother.x2;
        temp = smoother.temp;
        diagonal = smoother.diagonal;
        default_omega = smoother.default_omega;

        return *this;
    }
};

} // end namespace relaxation
} // end namespace cusp

#include <cusp/relaxation/detail/multi_jacobi.inl>


